package com.ocr.OcrImage;

import com.ocr.Utils.ImgUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenyuncong on 2019/6/26.
 */
public class ImagesTest {
    public static void main(String[] args) throws IOException {
        String inPath = "D:\\data\\orc\\";
        String outPath = "D:\\data\\orc\\code\\";

        for(int i=0;i<10;i++){
            extractPoint(inPath+"q"+i+".png",outPath+"q"+i+".png");
        }

    }

    public static void extractPoint(String inPath, String outPath) throws IOException {
        System.out.println(inPath);
        // 读取图片
        BufferedImage bufImage = ImageIO.read(new File(inPath));
        // 获取图片的宽高
        final int width = bufImage.getWidth();
        final int height = bufImage.getHeight();

        Map<Integer, List<Point>> map = new HashMap<>();
        int val = 0;
        List<Point> list = null;
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.println();
                val = bufImage.getRGB(col, row);
                if (map.containsKey(val)) {
                    map.get(val).add(new Point(col, row));
                } else {
                    list = new ArrayList<>();
                    list.add(new Point(col, row));
                    map.put(val, list);
                }
            }
        }
//        System.out.println(map.keySet().size());
        final int[] a = {0};
        final int[] b = {0};
        map.forEach((k, v) -> {
            if (v.size() > 10) {
                if (v.size() > a[0]) {
                    a[0] = v.size();
                    b[0] = k;
                }
//                System.out.println(k+"--"+v.size());
            }
        });
//        System.out.println(a[0] + " -- " + b[0]);
        BufferedImage bufImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        List<Point> pList = map.get(b[0]);
        int[] x = {0};
        int[] y = {0};
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                bufImage2.setRGB(col, row, 0x000000);

            }
        }
        map.get(b[0]).forEach(o -> {
            bufImage2.setRGB(o.x, o.y, b[0]);
        });

        ImageIO.write(bufImage2, "PNG", new File(outPath));

//        ImgUtils.removeBackground(outPath, ocrResult);
        //裁剪边角
//        ImgUtils.cuttingImg(ocrResult);
    }
}
